﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VehicleList.Models;

namespace VehicleList.Controllers
{ 
    public class VehicleController : Controller
    {
        VehicleListContext db = new VehicleListContext();

        //
        // GET: /Vehicle/

        public ViewResult Index()
        {
            var model = db.Vehicles;
            return View(model);
        }

        //
        // GET: /Vehicle/Details/5

        public ViewResult Details(int id)
        {
            Vehicle vehicle = db.Vehicles.Find(id);
            return View(vehicle);
        }

        //
        // GET: /Vehicle/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Vehicle/Create

        [HttpPost]
        public ActionResult Create(Vehicle vehicle)
        {
            if (ModelState.IsValid)
            {
                db.Vehicles.Add(vehicle);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(vehicle);
        }
        
        //
        // GET: /Vehicle/Edit/5
 
        public ActionResult Edit(int id)
        {
            Vehicle vehicle = db.Vehicles.Find(id);
            return View(vehicle);
        }

        //
        // POST: /Vehicle/Edit/5

        [HttpPost]
        public ActionResult Edit(Vehicle vehicle)
        {
            if (ModelState.IsValid)
            {
                db.Entry(vehicle).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(vehicle);
        }

        //
        // GET: /Vehicle/Delete/5
 
        public ActionResult Delete(int id)
        {
            Vehicle vehicle = db.Vehicles.Find(id);
            return View(vehicle);
        }

        //
        // POST: /Vehicle/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Vehicle vehicle = db.Vehicles.Find(id);
            db.Vehicles.Remove(vehicle);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}




//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Web;
//using System.Web.Mvc;
//using VehicleList.Models;
//using System.Data;

//namespace VehicleList.Controllers
//{
//    public class VehicleController : Controller
//    {
//        VehicleListContext db = new VehicleListContext();

//        //
//        // GET: /Vehicle/

//        public ActionResult Index()
//        {
//            using (var db = new VehicleListContext())
//            {
//                return View(db.Vehicles);
//            }
//            //return View(db.Vehicles);
//        }

//        //
//        // GET: /Vehicle/Details/5

//        public ActionResult Details(int id)
//        {
//            return View(db.Vehicles.Find(id));
//        }

//        //
//        // GET: /Vehicle/Create

//        public ActionResult Create()
//        {
//            return View();
//        } 

//        //
//        // POST: /Vehicle/Create

//        [HttpPost]
//        public ActionResult Create(Vehicle vehicle)
//        {
//            try
//            {
//                // TODO: Add insert logic here
//                db.Vehicles.Add(vehicle);
//                db.SaveChanges();

//                return RedirectToAction("Index");
//            }
//            catch
//            {
//                return View();
//            }
//        }
        
//        //
//        // GET: /Vehicle/Edit/5
 
//        public ActionResult Edit(int id)
//        {
//            return View(db.Vehicles.Find(id));
//        }

//        //
//        // POST: /Vehicle/Edit/5

//        [HttpPost]
//        public ActionResult Edit(int id, Vehicle vehicle)
//        {
//            try
//            {
//                // TODO: Add update logic here
//                db.Entry(vehicle).State = EntityState.Modified;
//                db.SaveChanges();
 
//                return RedirectToAction("Index");
//            }
//            catch
//            {
//                return View();
//            }
//        }

//        //
//        // GET: /Vehicle/Delete/5
 
//        public ActionResult Delete(int id)
//        {
//            return View(db.Vehicles.Find(id));
//        }

//        //
//        // POST: /Vehicle/Delete/5

//        [HttpPost]
//        public ActionResult Delete(int id, Vehicle vehicle)
//        {
//            try
//            {
//                // TODO: Add delete logic here
//                db.Entry(vehicle).State = EntityState.Deleted;
//                db.SaveChanges();
 
//                return RedirectToAction("Index");
//            }
//            catch
//            {
//                return View();
//            }
//        }
//    }
//}
